﻿using CampusTrade.Pojo;
using Furion.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CampusTrade.Dao
{
    public class GoodDao : ITransient
    {
        private readonly ISqlSugarRepository repository;
        private readonly SqlSugarClient db;
        public GoodDao(ISqlSugarRepository sqlSugarRepository)
        {
            repository = sqlSugarRepository;
            db = repository.Context;
        }

        public List<Classification> selectAllClassification()
        {
            try
            {
                return db.Queryable<Classification>().ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public Good select(long id)
        {
            try
            {
                return db.Queryable<Good>().First(it => it.Id == id);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public long add(Good good)
        {
            try
            {
                return db.Insertable(good).ExecuteReturnBigIdentity();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }

        public int delete(long id)
        {
            try
            {
                return db.Deleteable<Good>().In(id).ExecuteCommand();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }

        public int updateStatus(Good good)
        {
            try
            {
                return db.Updateable(good).UpdateColumns(it => new { it.StatusId }).ExecuteCommand();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }
    }
}
